import { container } from 'tsyringe';
import { getAllOnDestory, getAllOnInit } from '../../tokens/core';
import { uiInit } from './uiInit';
import { uiRegister } from './uiRegister';

export function uiBootstrap() {
    try {
        if (auto.service === null) {
            app.startActivity({
                action: "android.settings.ACCESSIBILITY_SETTINGS"
            });
            return;
        }
        auto.waitFor();
        if (auto.service == null) {
            toast("请先开启无障碍服务！");
            exit()
            return;
        }
        if (!floaty.checkPermission()) {
            floaty.requestPermission()
            exit()
            return;
        }
        if (device.height === 0 || device.width === 0) {
            toast(
                'Failed to get the screen size. ' +
                'Please try restarting the service or re-installing Hamibot'
            );
            exit()
        } else {
            console.log("Screen size: " + device.height + " x " + device.width);
        }
        uiRegister();
        uiInit();
        const onInits = getAllOnInit();
        onInits.map(init => init());
        const interval = setInterval(() => { }, 1000)
        events.on('exit', () => {
            const onDestory = getAllOnDestory();
            onDestory.map(destory => destory());
            container.dispose();
            clearInterval(interval)
        })
    } catch (e) {
        toast(e.message)
    }
}
